package test.okhttp

import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import org.junit.jupiter.api.Test

class OKHttpTest {

    private val client = OkHttpClient()

    @Test
    fun testGet() {
        val url = "https://im.qq.com"
        val request: Request = Request.Builder()
            .url(url)
            .build()
        client.newCall(request).execute().use {
                response ->  println(response.body?.string())
        }
    }

    @Test
    fun testPost() {
        val url = "http://www.roundsapp.com/post"
        val content = ("{'winCondition':'HIGH_SCORE}")
        val body: RequestBody = RequestBody.create("application/json; charset=utf-8".toMediaType(), content)
        val request: Request = Request.Builder()
            .url(url)
            .post(body)
            .build()
        client.newCall(request).execute().use {
                response ->  println(response.body?.string())
        }
    }
}